﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.ETL.Domain.DataVerification;
using CPPEI.ETL.Domain.Services;
using CPPEI.ETL.Domain.TaskRuntime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CPPEI.ETL.TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            CPPEI.Logger.InitByLogDirectory(AppDomain.CurrentDomain.BaseDirectory);
            //TestWSUtil.TestIsSupportResultType();
            //TestWSUtil.Run();
            //TestTaskPool.Test();
            //TestJs.Test();
            //IDataVerification dv = new ScriptDataVerification("data.Get('a') > (data.Get('b')+data.Get('c'))");
            //IDataVerification dv = new SPDataVerification("[SP_TESTDV]");

            //TestParameter();
            TestForeachArrayParameter();
            Console.ReadKey();
            //TestRun();
            //TestGetData();
            //TestGetData2();

            //bool ret1 = IsName("abc123");
            //bool ret2 = IsName("abc-123");
            //bool ret3 = IsName("123abc-123");
            //bool ret4 = IsName("123abc123");
            //bool ret5 = IsName("abc_123");
            //bool ret6 = IsName("abc_1.23");
            //bool ret7 = IsName("a");
            //bool ret8 = IsName("c1");
        }

        public static bool IsName(string name)
        {
            Regex regex = new Regex(@"^[a-zA-Z][a-zA-z0-9_]*$");
            if (regex.IsMatch(name))
            {
                return true;
            }
            return false;
        }

        private static void TestGetData2()
        {
            CPPEI.ETL.DSImplement.SQL.SQLDtSerProxy proxy = new DSImplement.SQL.SQLDtSerProxy();

            DataSourceInfo dsi = new DataSourceInfo(".,dataDB", true, "sa", "123456789");
            dsi.DataSource = "select * from Table_1";
            dsi.DataSourceType = CPPEI.ETL.DSImplement.DEF.SQLSourceType.Statement;
            Dictionary<string, object> args = new Dictionary<string, object>();
            IData data = proxy.GetData(dsi, args);
        }


        private static void TestGetData()
        {
            CPPEI.ETL.DSImplement.SQL.SQLDtSerProxy proxy = new DSImplement.SQL.SQLDtSerProxy();

            DataSourceInfo dsi = new DataSourceInfo(".,dataDB", true, "sa", "123456789");
            dsi.DataSource = "SP_GetData";
            dsi.DataSourceType = CPPEI.ETL.DSImplement.DEF.SQLSourceType.StoredProcedure;
            Dictionary<string, object> args = new Dictionary<string,object>();
            args.Add("P1", 2);
            args.Add("p2", "abc");
            IData data = proxy.GetData(dsi, args);
        }

        private static void TestRun()
        {
            TaskService taskService = new TaskService();
            taskService.RunById("abc", new Guid("DFDB2822-826A-4410-864B-63D383EF8C2A"));
        }

        private static void TestParameter()
        {
            RTParameterContext context = new RTParameterContext();
            context.Open();

            string value = context.Run("DateTime.Now().AddDays(1)");
            string value2 = context.Run("DateTime.Now().AddDays(-10).ToString(\"yyyy-MM-dd\")");

            string value3 = context.Run("DateTime.Now().AddDays(-10).ToString('yyyy-MM-dd')");
            Console.WriteLine(value3);

            context.Close();
        }

        private static void TestCondition()
        {
            string condition = "CPPEI.ETL.TestApp,CPPEI.ETL.TestApp.CodeDV";
            string[] conditions = condition.Split(new char[] { ',' });
            IDataVerification dv = PlugInManager.Create(conditions[0], conditions[1]);

            if(dv is IDataVerificationContext)
            {
                (dv as IDataVerificationContext).Open();
            }

            List<Row> rows = new List<Row>();
            Row row1 = new Row(1);
            Row.Add(row1, "a", "1");
            Row.Add(row1, "b", "2");
            Row.Add(row1, "c", "3");
            rows.Add(row1);

            Row row2 = new Row(2);
            Row.Add(row2, "a", "5");
            Row.Add(row2, "b", "2");
            Row.Add(row2, "c", "1");
            rows.Add(row2);

            for (int index = 0; index < rows.Count; index++)
            {
                bool isPass = dv.Verify(rows[index]);
                if(isPass)
                {
                    Console.WriteLine("true");
                }
                else
                {
                    Console.WriteLine("false");
                }
            }

            if (dv is IDataVerificationContext)
            {
                (dv as IDataVerificationContext).Close();
            }

            Console.ReadKey();
        }

        private static void TestForeachArrayParameter()
        {

            List<string> foreachParameterNameList = new List<string>();
            List<string[]> foreachParameterArrayList = new List<string[]>();
            foreachParameterNameList.Add("A");
            foreachParameterArrayList.Add(new string[] { "A-v1", "A-v2", "A-v3" });
            foreachParameterNameList.Add("B");
            foreachParameterArrayList.Add(new string[] { "B-v1", "B-v2", "B-v3", "B-v4" });
            foreachParameterNameList.Add("C");
            foreachParameterArrayList.Add(new string[] { "C-v1", "C-v2" });

            Dictionary<string, string> args = new Dictionary<string, string>();

            int totalForCount = 1;
            for (int index = 0; index < foreachParameterNameList.Count; index++)
            {
                totalForCount = totalForCount * foreachParameterArrayList[index].Length;
            }
            for (int loopIndex = 0; loopIndex < totalForCount; loopIndex++)
            {
                int remainder = loopIndex;
                int remainderCount = totalForCount;
                for (int index = 0; index < foreachParameterNameList.Count; index++)
                {
                    int seed = remainder / (remainderCount / foreachParameterArrayList[index].Length);
                    remainder = remainder % (remainderCount / foreachParameterArrayList[index].Length);
                    remainderCount = remainderCount / foreachParameterArrayList[index].Length;
                    string foreachParameterName = foreachParameterNameList[index];
                    string foreachParameterValue = foreachParameterArrayList[index][seed];
                    if (args.ContainsKey(foreachParameterName))
                    {
                        args[foreachParameterName] = foreachParameterValue;
                    }
                    else
                    {
                        args.Add(foreachParameterName, foreachParameterValue);
                    }
                    //dataList.Add(proxy.GetData(dataSourceInfo, new Dictionary<string, object>(args)));
                }
                DumpDict(args);
            }
        }
        private static void DumpDict(Dictionary<string, string> args)
        {
            StringBuilder builder = new StringBuilder();
            foreach (var key in args.Keys)
            {
                builder.AppendFormat("{0}={1},", key, args[key]);
            }
            Console.WriteLine(builder.ToString());
        }
    }
}
